package com.cn.xact.ty_treasured_book.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.TYApplicationModule;
import com.cn.xact.ty_treasured_book.TYApplicationState;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class AddEntryActivity extends Activity {

    private ListView mEntryList;
    private EntryListViewAdapter mEntryListViewAdapter;
    public static TYApplicationState app;
    private boolean mIsOnL1;
    private String mCurL2TopName;

    private List<Map<String, Object>> listItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_add_entry);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        app = new TYApplicationState(this);

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.main_add);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);
        title1.setText(R.string.add_entry_title);

        loadL1Items();
    }

    private void refreshView() {
        if (mIsOnL1)
            loadL1Items();
        else
            loadL2Items(mCurL2TopName);
    }

    private void loadL1Items() {
        mEntryList = (ListView)findViewById(R.id.entry_add_view);
        listItems = getListItems();
        mEntryListViewAdapter = new EntryListViewAdapter(this, listItems);
        mEntryList.setAdapter(mEntryListViewAdapter);
        mIsOnL1 = true;
    }

    private void loadL2Items(String name) {
        listItems = getListItemsforTop(name);
        mEntryListViewAdapter = new EntryListViewAdapter(this, listItems);
        mEntryList.setAdapter(mEntryListViewAdapter);
        mIsOnL1 = false;
        mCurL2TopName = name;
    }

    private List getListItems() {
        List<Map<String, Object>> innnerListItems = new ArrayList<Map<String, Object>>();
        for(TYApplicationModule module : app.getModuleList()) {
            if (!module.isChild()) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("title", module.getDisplayName());
                map.put("isValid", module.isEnable());
                map.put("hasChild", module.hasChild());
                innnerListItems.add(map);
            }
        }
        return innnerListItems;
    }

    private List getListItemsforTop(String topName) {
        List<Map<String, Object>> innnerListItems = new ArrayList<Map<String, Object>>();
        for(TYApplicationModule module : app.getModuleList()) {
            if ((module.getDisplayName().equals(topName)) && (module.hasChild())) {
                for (TYApplicationModule child : module.getChilds()) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("title", child.getDisplayName());
                    map.put("isValid", child.isEnable());
                    map.put("hasChild", "false");
                    innnerListItems.add(map);
                }
                break;
            }
        }
        return innnerListItems;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent tmp = new Intent(AddEntryActivity.this, MainActivity.class);
            startActivity(tmp);
            AddEntryActivity.this.finish();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class EntryListViewAdapter extends BaseAdapter {

        private Context context;
        private List<Map<String, Object>> listItems;
        private LayoutInflater listContainer;

        public final class ListItemView{
            public TextView title;
            public ImageView icon;
     }

        public EntryListViewAdapter(Context context, List<Map<String, Object>> listItems) {
            this.context = context;
            listContainer = LayoutInflater.from(context);
            this.listItems = listItems;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listItems.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return listItems.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ListItemView  listItemView = null;
            if (convertView == null) {
                listItemView = new ListItemView();
                convertView = listContainer.inflate(R.layout.add_entry_item, null);

                listItemView.title = (TextView)convertView.findViewById(R.id.add_entry_item_title);
                listItemView.icon = (ImageView)convertView.findViewById(R.id.add_entry_item_icon);
                convertView.setTag(listItemView);
            }else {
                listItemView = (ListItemView)convertView.getTag();
            }

            final String title = (String)listItems.get(position).get("title");
            listItemView.title.setText(title);

            if ((boolean)listItems.get(position).get("hasChild").equals(true)) {
                listItemView.icon.setImageResource(R.drawable.extend_arrow);

                listItemView.icon.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        loadL2Items(title);
                    }

                });
            } else {
                if ((boolean)listItems.get(position).get("isValid").equals(true)) {
                    listItemView.icon.setImageResource(R.drawable.plus);

                    listItemView.icon.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            // TODO Auto-generated method stub
                            app.removeModule(title);
                            ((ImageView)arg0).setImageResource(R.drawable.add);
                            refreshView();
                        }

                    });
                } else {
                    listItemView.icon.setImageResource(R.drawable.add);

                    listItemView.icon.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            // TODO Auto-generated method stub
                            app.addModule(title);
                            ((ImageView)arg0).setImageResource(R.drawable.plus);
                            refreshView();
                        }
                    });
                }
            }
            return convertView;
        }

    }

}
